//
//  DDNewCattleViewController.m
//  DingDongZP
//
//  Created by Ray on 16/6/7.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDNewCattleViewController.h"
#import "DDJobfinderDetailViewController.h"
#import "DDBossPublishJobViewController.h"
#import "DDJobfindersService.h"
#import "DDBossJobService.h"
#import "DDDiscoverJobFinderCell.h"
#import "DDBossPublishedJobModel.h"
#import "DDBossInfoService.h"

@interface DDNewCattleViewController ()
<UITableViewDataSource,
UITableViewDelegate>

@property (nonatomic, strong) UILabel     *lbNull;
@property (nonatomic, strong) UITableView *tbCattles;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (strong, nonatomic) DDJobfindersService *jobfindersService;
@property (strong, nonatomic) DDBossJobService    *jobService;

@end

@implementation DDNewCattleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.jobfindersService = [[DDJobfindersService alloc] init];
    self.jobService = [[DDBossJobService alloc] init];
    self.dataSource = [NSMutableArray new];
    
    self.title = @"新牛人";
    
    self.lbNull = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    self.lbNull.font = [UIFont systemFontOfSize:17.0];
    self.lbNull.textColor = COLOR_FONT_LIGHT_GRAY;
    self.lbNull.text = @"暂无数据";
    self.lbNull.textAlignment = NSTextAlignmentCenter;
    self.lbNull.center = CGPointMake(self.view.width/2, self.view.height/2);
    //    self.lbNull.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    self.lbNull.hidden = YES;
    [self.view addSubview:_lbNull];
    
    _tbCattles = [[UITableView alloc] init];
    _tbCattles.tag = 2;
    _tbCattles.delegate = self;
    _tbCattles.dataSource = self;
    _tbCattles.backgroundColor = COLOR_BACKGROUND_GRAY;
    _tbCattles.rowHeight = HEIGHT_DISCOVER_CELL;
    _tbCattles.tableFooterView = [[UIView alloc] init];
    
    UINib *cellNib = [UINib nibWithNibName:@"DDDiscoverJobCell" bundle:nil];
    [_tbCattles registerNib:cellNib forCellReuseIdentifier:DDDiscoverJobCellIdentifier];
    
    _tbCattles.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbCattles.frame = self.view.bounds;
    _tbCattles.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_tbCattles];
    
    [self.view bringSubviewToFront:_lbNull];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [self getMyPublishedJobs];
}

#pragma mark - data source
/** 获取我发布的职位 */
- (void)getMyPublishedJobs {
    WEAKSELF
    
    [[LDBossInfoNet sharedInstance] viewMyselfAllJobsKinds:^(NSArray<NSDictionary *> * _Nonnull dictArr) {
        
        if (dictArr.count == 0) {
            
            weakSelf.lbNull.hidden = NO;
            
        } else {
            
            weakSelf.lbNull.hidden = YES;
        }
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf updateWithJobInfo:dictArr];
        }];
        
    } failure:^(id  _Nonnull error) {
        
    }];
}


- (void)updateWithJobInfo:(NSArray *)jobinf
{
    if (jobinf.count > 0) {
        NSDictionary *info = jobinf[0];
        NSString *jobid = info[@"jobid"];
        
        [self showLoading];
        
        WEAKSELF
        
        [self.jobService  queryJobfindersWithJobId:jobid order:@"latest" page:@"0" SuccessBlock:^(id responseObject) {
            [weakSelf hideLoading];
            if ([weakSelf isValidateResponseData:responseObject]) {
                // 请求成功
                DDJobfindersBaseModel *model = [DDJobfindersBaseModel yy_modelWithJSON:responseObject[@"data"]];
                
                
                if ([model.jobfinders count] == 0) {
                    weakSelf.lbNull.hidden = NO;
                    
                } else {
                    
                    weakSelf.lbNull.hidden = YES;
                    [weakSelf.dataSource addObjectsFromArray:model.jobfinders];
                    [weakSelf.tbCattles reloadData];
                }
                
            } else {
                //            [weakSelf responseUnValidate:responseObject];
            }
        } failBlock:^(id responseObject, NSError *error) {
            [weakSelf requestFaild];
        }];
    }
    
}

#pragma mark - table view

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDBossRecJobFinderCell *cell;
    cell = (LDBossRecJobFinderCell *)[tableView dequeueReusableCellWithIdentifier:@"DDBossDiscoverCellIdentifier"];
    
    if (cell == nil) {
        
        cell = [[LDBossRecJobFinderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DDBossDiscoverCellIdentifier"];
    }
    
    if (indexPath.row < self.dataSource.count) {
        
        cell.models = (DDJobfinderDetail *)self.dataSource[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < self.dataSource.count) {
        DDJobfinderDetail *jobfinderDetail = self.dataSource[indexPath.row];
        
        LDJobfinderDetailBaseVCInAll_Show *vc = [[LDJobfinderDetailBaseVCInAll_Show alloc] init];
        vc.userID = jobfinderDetail.userid;
        
        [self.navigationController pushViewController:vc animated:YES];
        [[DDBossInfoService sharedService] addActivenessWithType:kActivityCommunicate callback:^(BOOL succ, id data) {
            
        }];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
